package com.rykj.qxj.rest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.rykj.qxj.annotation.EnableSystemLog;
import com.rykj.qxj.server.common.exception.ApiAbilityStaticsticsIsNullException;
import com.rykj.qxj.server.common.exception.AppIdIsNullException;
import com.rykj.qxj.server.common.exception.ApplyIsNotExistException;
import com.rykj.qxj.server.dao.DataMartMapper;
import com.rykj.qxj.server.model.entity.ApiAbilityStatistics;
import com.rykj.qxj.server.model.entity.Notice;
import com.rykj.qxj.server.services.IApiAbilityStatisticsService;
import com.rykj.qxj.server.services.IApplyManageService;
import com.rykj.qxj.server.services.IMetadataManageService;
import com.rykj.qxj.server.util.date.DateUtils;
import com.rykj.qxj.util.Result;
import com.techindata.cacheredis.CacheGet;
import com.techindata.cacheredis.CacheZSet;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

@Log4j2
@RestController
@RequestMapping("/v1/visualScreenManage")
@Api(tags = "可视化大屏管理")
public class VisualScreenManageController {
	@Autowired
	private IMetadataManageService metadataManageService;

	@Autowired
	private IApiAbilityStatisticsService apiAbilityStatisticsService;
	
	@Autowired
	private IApplyManageService applyManageService;

	
	private static final String API_TOTAL_VISITCOUNT= "apiTotalvisitCount";  //所有接口总的访问量
	private static final String API_TODAY_VISITCOUNT= "apiTodayvisitCount";   //今日接口访问量
	
	private static final String ALL_APPLICATION_PERMONTH= "allApplicationPerMonth";   //所有应用每月访问量

	
	@GetMapping("/getDataDirectoryList")
	@ApiOperation(value = "首页-数据目录占比", notes = "数据目录占比")
	public Result selectDataCatalogue() {
		try {
			List<Map<String, Object>> selectDataCalogau = metadataManageService.selectDataCalogau();
			return new Result(selectDataCalogau);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "查询失败");
		}
	}
	@GetMapping("/getApiCount")
	@ApiOperation(value = "首页-获取接口总的访问量和当日访问量,apiTotalVisit:接口总的访问量,apiTodayTotalVisit:接口当日总的访问量", notes = "获取接口总的访问量和当日访问量,apiTotalVisit:接口总的访问量,apiTodayTotalVisit:接口当日总的访问量")
	public Result getApiCount() {
		try {
			SimpleDateFormat sdfToday=new SimpleDateFormat("yyyy-MM-dd"); 
			Map<String,Object> countMap=new HashMap<String,Object>();
			String value = CacheGet.getValue(API_TOTAL_VISITCOUNT);
			if(StringUtils.isBlank(value)) {
				countMap.put("apiTotalVisit", 0);
			}else {
				countMap.put("apiTotalVisit", value);
			}

			double zscore = CacheZSet.zscore(API_TODAY_VISITCOUNT, sdfToday.format(new Date()));
			if(zscore==-1.0) {
				countMap.put("apiTodayTotalVisit", 0);
				CacheZSet.put(API_TODAY_VISITCOUNT,sdfToday.format(new Date()),0);
			}else {
				countMap.put("apiTodayTotalVisit", zscore);
			}
			return new Result(countMap);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "查询失败");
		}
	}
	@GetMapping("/getApplicationNearlyYearDataList")
	@ApiOperation(value = "应用-近一年的应用访问量", notes = "应用-近一年的应用访问量")
	public Result selectApplicationNearlyYearDataList() {
		try {
			SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM");
			List<Map<String, Object>> resultMapList=new ArrayList<Map<String,Object>>();
			for(int i=0;i<12;i++) {
				Map<String,Object> map=new HashMap<>();
				Date addHowMonth = DateUtils.addHowMonth(new Date(), -i);
				map.put("month", sdf3.format(addHowMonth));
				double zscore = CacheZSet.zscore(ALL_APPLICATION_PERMONTH, sdf3.format(addHowMonth));
				if(zscore==-1.0) {
					map.put("value", "0");
					CacheZSet.put(ALL_APPLICATION_PERMONTH, sdf3.format(addHowMonth),0);
				}else {
					map.put("value", zscore);
					
				}
				resultMapList.add(map);
			}
			 Collections.sort(resultMapList, new Comparator<Map<String,Object>>(){
					@Override
					public int compare(Map<String, Object> o1, Map<String, Object> o2) {
						int temp=Integer.valueOf(o1.get("month").toString().replaceAll("-", ""))>Integer.valueOf(o2.get("month").toString().replaceAll("-", ""))?-1:0;
						return temp;
					}
			});
			return new Result(resultMapList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "查询失败");
		}
	}
	
	@GetMapping("/getApplicationInvocationApiCount")
	@ApiOperation(value = "应用-调用接口（个）今日、本月、全年 type=1表示今日,type=2表示本月,type=3表示全年", notes = "应用调用接口（个）今日、本月、全年 type=1表示今日,type=2表示本月,type=3表示全年")
	@ApiImplicitParam(name = "type", value = "统计类型",required = true,paramType = "query")
	public Result selectApplicationInvocationApiCount(@RequestParam("type") String type) {
		try {
		    List<Map<String, Object>> resultList= apiAbilityStatisticsService.selectApplicationInvocationApiCount(type);
			return new Result(resultList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "查询失败");
		}
	}
	@GetMapping("/getApiInvocationCountTop5")
	@ApiOperation(value = "应用-接口调用Top5   type=1表示今日,type=2表示本月,type=3表示全年", notes = "应用-接口调用Top5   type=1表示今日,type=2表示本月,type=3表示全年")
	//@ApiImplicitParam(name = "type", value = "统计类型",required = true,paramType = "query")
	//public Result selectApiInvocationCountTop5(@RequestParam("type") String type) {
	public Result selectApiInvocationCountTop5() {
		try {
			List<Map<String, Object>> resultList= apiAbilityStatisticsService.selectApiInvocationCountTop5();
			return new Result(resultList);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "查询失败");
		}
	}
	@GetMapping("/getApplicationIndicatorsData")
	@ApiOperation(value = "应用-指标数据  7dayaddApply:7日新增应用,applyCount:应用总数,applyTotalVisit:应用调用总数,onlineApplyCount:上线应用数,notOnlineApplyCount:未上线应用数", notes = "应用-指标数据  7dayaddApply:7日新增应用,applyCount:应用总数,applyTotalVisit:应用调用总数,onlineApplyCount:上线应用数,notOnlineApplyCount:未上线应用数")
	public Result selectApplicationIndicatorsData() {
		try {
			Map<String,Object> countMap=new HashMap<String,Object>();
			String value = CacheGet.getValue(API_TOTAL_VISITCOUNT);
			if(StringUtils.isBlank(value)) {
				countMap.put("applyTotalVisit", 0);
			}else {
				countMap.put("applyTotalVisit", value);
			}
			countMap.put("7dayaddApply", applyManageService.selectNewAddApplyNear7day());
			countMap.put("applyCount", applyManageService.selectCountApply());
			countMap.put("onlineApplyCount", applyManageService.selectCountOnlineApply());
			countMap.put("notOnlineApplyCount", applyManageService.selectCountNotOnlineApply());
			return new Result(countMap);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "查询失败");
		}
	}
	
}
